﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kxcxw.Data.Entities;
using kxcxw.Data;
using Webdiyer.WebControls.Mvc;
using System.Web.Security;
using kxcxw.Data.Common;

namespace KXCXW.Web.Areas.Admin.Controllers
{
    public class AdMController : Controller
    {
        private KXCXWDBContext db = new KXCXWDBContext();

        //
        // GET: /Admin/Default1/
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Index(string sortOrder, string searchString, int? page = 1)
        {
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
            var Adinfo = from s in db.AdApplys select s;
            string shoptype = "";
            string status = "0";
            if (searchString != null && searchString != "")
            {
                string[] searcharr = searchString.Split(';');
                if (!String.IsNullOrEmpty(searchString))
                {
                    foreach (string arr1 in searcharr)
                    {
                        string[] arr2 = arr1.Split(':');
                        string feidName = arr2[0];
                        string value = arr2[1];
                        if (feidName == "title")
                        {
                            Adinfo = Adinfo.Where(s => s.AltText.ToUpper().Contains(value));
                            ViewBag.searchTitle = value;
                        }
                        else if (feidName == "shoptype" && value != "")
                        {
                            string type = value == "1" ? "W" : "S";
                            Adinfo = Adinfo.Where(s => s.AdBlockId.Contains(type));
                            shoptype = value;
                        }
                        else if (feidName == "status" && value != "")
                        {
                            int statusInt = Convert.ToInt32(value);
                            Adinfo = Adinfo.Where(s => s.Status == statusInt);
                            status = value;
                        }
                    }
                }
            }
            else
            {
                Adinfo = Adinfo.Where(s => s.Status == 0);
            }
            List<SelectListItem> shoptypelist = new List<SelectListItem>();
            shoptypelist.Add(new SelectListItem { Value = "1", Text = "网店" });
            shoptypelist.Add(new SelectListItem { Value = "2", Text = "实体店" });
            if (shoptype != "")
                shoptypelist[Convert.ToInt32(shoptype) - 1].Selected = true;
            ViewBag.ShopTypeList = shoptypelist;

            List<SelectListItem> statuslist = new List<SelectListItem>();
            statuslist.Add(new SelectListItem { Value = "0", Text = "等待审核" });
            statuslist.Add(new SelectListItem { Value = "1", Text = "通过审核" });
            statuslist.Add(new SelectListItem { Value = "2", Text = "已删除" });
            statuslist.Add(new SelectListItem { Value = "3", Text = "未通过审核" });
            statuslist[Convert.ToInt32(status)].Selected = true;
            ViewBag.StatusList = statuslist;

            switch (sortOrder)
            {
                case "Date":
                    Adinfo = Adinfo.OrderBy(s => s.CreateDate);
                    break;
                default:
                    Adinfo = Adinfo.OrderBy(s => s.CreateDate);
                    break;
            }
            int pageSize = 15;
            int pageIndex = page ?? 1;
            PagedList<AdApply> temp = Adinfo.ToPagedList(pageIndex, pageSize);
            if (Request.IsAjaxRequest())
                return PartialView("List", temp);
            return View(temp);
        }
        [Authorize(Roles = DataConstants.Admin)]
        public string DoneShengHe(string adId,string status)
        {
            try
            {
                AdApply ad = (from q in db.AdApplys
                              where q.AdId == adId
                              select q).FirstOrDefault();
                db.Entry(ad).State = EntityState.Modified;
                ad.Status = Convert.ToInt32(status);
                ad.ApprovalDate = DateTime.Now;
                ad.ApprovalUser = Membership.GetUser().ProviderUserKey.ToString();
                db.SaveChanges();
                return "true";
            }
            catch (Exception ex)
            {
                return "false";
            }
        }

        //
        // GET: /Admin/Default1/Details/5
        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Details(string id)
        {
            AdApply adapply = db.AdApplys.Find(id);
            return View(adapply);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}